package com.aaa.pdfdemo;

import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.*;
import com.itextpdf.text.*;
//import org.apache.commons.lang.StringUtils;

//import javax.swing.border.Border;

public class ItextBuilder {

    private static final Float PADDING_WIDTH = 12.0f;
    private static final int BORDER_LEFT = 4;
    private static final int BORDER_RIGHT = 8;
    private  static final BaseColor BORDER_COLOR = new BaseColor(234,244,250);

    public static PdfPTable newPdftable(int columm){
        PdfPTable table = new PdfPTable(columm);
        table.setTotalWidth(460);
        table.setLockedWidth(true);
        table.setWidthPercentage(95);
        table.setSpacingBefore(10f);
        table.setSpacingAfter(10f);
        table.setSplitRows(true);
        table.getDefaultCell().disableBorderSide(15);
        table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
        table.setKeepTogether(true);
        table.setSplitLate(false);
        return table;
    }

    public static PdfPTable newPdftable2(int columm){
        PdfPTable table = new PdfPTable(columm);
        //table.setTotalWidth(500);
        //table.setLockedWidth(true);
        table.setWidthPercentage(97);
        table.setSpacingBefore(10f);
        table.setSpacingAfter(10f);
        table.setSplitRows(true);
        table.getDefaultCell().disableBorderSide(15);
        table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
        table.setKeepTogether(true);
        table.setSplitLate(false);
        return table;
    }

    public static void initPdfPCell(PdfPCell cell){
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.disableBorderSide(15);
    }

    public static Image newImage(String path,float newWidth,float newHeight) throws Exception{
        Image image = Image.getInstance(path);
        image.scaleAbsolute(newWidth, newHeight);
        return image;
    }

    public static PdfPCell getPdfCell(String value,Font font){
         PdfPCell cell = new PdfPCell(new Paragraph(value,font));
         cell.setHorizontalAlignment(Element.ALIGN_LEFT);
         cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
         cell.setBackgroundColor(BaseColor.WHITE);
         cell.setBorderColor(BORDER_COLOR);
         cell.setMinimumHeight(25);
         return cell;
    }

    public static PdfPCell mergeRow (String str,Font font, int i ){
        PdfPCell cell = new PdfPCell(new Paragraph(str,font));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setBackgroundColor(BaseColor.WHITE);
        cell.setBorderColor(BORDER_COLOR);
        cell.setBorderWidth(1.5F);
        //将该单元格所在列包括该单元格在内的i行单元格合并
        cell.setRowspan(i);
        return cell;
    }

    public static PdfPCell mergeRow2 (String str,Font font, int i ){
        PdfPCell cell = new PdfPCell(new Paragraph(str,font));
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setBackgroundColor(BaseColor.WHITE);
        cell.setBorderColor(BORDER_COLOR);
        //将该单元格所在列包括该单元格在内的i行单元格合并
        cell.setRowspan(i);
        return cell;
    }

    public static PdfPCell mergeClo (String str,Font font, int i ){
        PdfPCell cell = new PdfPCell(new Paragraph(str,font));
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setBackgroundColor(BaseColor.WHITE);
        cell.setBorderColor(BORDER_COLOR);
        cell.setMinimumHeight(25);
        //将该单元格所在列包括该单元格在内的i列单元格合并
        cell.setColspan(i);
        return cell;
    }

    //合并多个单元格，隐藏边框
    public static PdfPCell mergeCenterClo (String str,Font font, int i ,String type,int value){
        PdfPCell cell = new PdfPCell(new Paragraph(str,font));
        cell.disableBorderSide(value);
        if("left".equals(type)) {
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        } else if("center".equals(type)){
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        } else if("right".equals(type)){
            cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        }
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setMinimumHeight(25);
        //将该单元格所在列包括该单元格在内的i列单元格合并
        cell.setColspan(i);
        cell.setBackgroundColor(BaseColor.WHITE);
        cell.setBorderColor(BORDER_COLOR);
        cell.setBorderWidth(1.5F);
        return cell;
    }

    public static PdfPCell mergeCenterClo2 (String str,Font font, int i ,String type,int value){
        PdfPCell cell = new PdfPCell(new Paragraph(str,font));
        cell.disableBorderSide(value);
        if("left".equals(type)) {
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        } else if("center".equals(type)){
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        } else if("right".equals(type)){
            cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        }
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setMinimumHeight(25);
        //将该单元格所在列包括该单元格在内的i列单元格合并
        cell.setColspan(i);
        cell.setBackgroundColor(BaseColor.WHITE);
        cell.setBorderColor(BORDER_COLOR);
        return cell;
    }

    public static PdfPCell getPdfNameCell(String value,Font font){
        PdfPCell cell = new PdfPCell(new Paragraph(value,font));
        cell.setBorderWidth(1.5F);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setMinimumHeight(25);
        cell.setBackgroundColor(BaseColor.WHITE);
        cell.setBorderColor(BORDER_COLOR);
        cell.disableBorderSide(BORDER_RIGHT);
        cell.setPaddingLeft(PADDING_WIDTH);
        return cell;
    }

    public static PdfPCell getPdfNameCell(String value,Font font ,int col){
        PdfPCell cell = new PdfPCell(new Paragraph(value,font));
        cell.setBorderWidth(1.5F);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setMinimumHeight(25);
        cell.setBackgroundColor(BaseColor.WHITE);
        cell.setBorderColor(BORDER_COLOR);
        //cell.disableBorderSide(border);
        cell.setColspan(col);
        cell.disableBorderSide(BORDER_RIGHT);
        cell.setPaddingLeft(PADDING_WIDTH);
        return cell;
    }

    public static PdfPCell getPdfPicNameCell(String value,Font font ,int col){
        PdfPCell cell = new PdfPCell(new Paragraph(value,font));
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
//        cell.setMinimumHeight(25);
        cell.setBackgroundColor(BaseColor.WHITE);
        //cell.setBorderColor(BORDER_COLOR);
        //cell.disableBorderSide(border);
        cell.setColspan(col);
        cell.disableBorderSide(15);
        //cell.setPaddingLeft(PADDING_WIDTH);
        return cell;
    }

    public static PdfPCell getPdfNameCell(String value,Font font ,int col,int border){
        PdfPCell cell = new PdfPCell(new Paragraph(value,font));
        cell.setBorderWidth(1.5F);
        //cell.setBorderWidthTop(topPadding);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setMinimumHeight(25);
        cell.setBackgroundColor(BaseColor.WHITE);
        cell.setBorderColor(BORDER_COLOR);
        cell.disableBorderSide(border);
        cell.setColspan(col);
        cell.disableBorderSide(BORDER_RIGHT);
        cell.setPaddingLeft(PADDING_WIDTH);
        return cell;
    }



    public static PdfPCell getPdfValueCell(String value,Font font){
        PdfPCell cell = new PdfPCell(new Paragraph(value,font));
        cell.setBorderWidth(1.5F);
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setMinimumHeight(25);
        cell.setBackgroundColor(BaseColor.WHITE);
        cell.setBorderColor(BORDER_COLOR);
        cell.disableBorderSide(BORDER_LEFT);
        cell.setPaddingRight(PADDING_WIDTH);
        return cell;
    }


    public static PdfPCell getPdfValueCell(String value,Font font,int col){
        PdfPCell cell = new PdfPCell(new Paragraph(value,font));
        cell.setBorderWidth(1.5F);
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setMinimumHeight(25);
        cell.setBackgroundColor(BaseColor.WHITE);
        cell.setBorderColor(BORDER_COLOR);
        cell.setColspan(col);
        cell.setPaddingRight(PADDING_WIDTH);
        cell.disableBorderSide(BORDER_LEFT);
        return cell;
    }

    public static PdfPCell getPdfValueCell(String value,Font font,int col,float paddingWidth){
        PdfPCell cell = new PdfPCell(new Paragraph(value,font));
        cell.setBorderWidth(1.5F);
//        if (StringUtils.isNotBlank(value) && value.length() > 48) {
//            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
//        } else {
//            cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
//        }
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setMinimumHeight(25);
        cell.setBackgroundColor(BaseColor.WHITE);
        cell.setBorderColor(BORDER_COLOR);
        cell.setColspan(col);
        cell.setPaddingRight(PADDING_WIDTH);
        cell.disableBorderSide(BORDER_LEFT);
        cell.setPaddingTop(paddingWidth);
        cell.setPaddingBottom(paddingWidth);
        return cell;
    }

    public static PdfPCell getPdfValueCell(String value,Font font,int col,int border){
        PdfPCell cell = new PdfPCell(new Paragraph(value,font));
        cell.setBorderWidth(1.5F);
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setMinimumHeight(25);
        cell.setBackgroundColor(BaseColor.WHITE);
        cell.setBorderColor(BORDER_COLOR);
        cell.disableBorderSide(border);
        cell.setColspan(col);
        cell.setPaddingRight(PADDING_WIDTH);
        cell.disableBorderSide(BORDER_LEFT);
        return cell;
    }

    public static PdfPTable newPicPdftable(int columm){
        PdfPTable table = new PdfPTable(columm);
        //table.setTotalWidth(543.9f);
        //table.setLockedWidth(true);
        table.setWidthPercentage(93.5f);
//        table.setSpacingBefore(10f);
//        table.setSpacingAfter(10f);
       // table.setSplitRows(true);
        table.getDefaultCell().disableBorderSide(15);
        table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
        //table.setKeepTogether(true);
        //table.setSplitLate(false);
        return table;
    }
}
